package Services

import (
	"github.com/gin-gonic/gin"
	"net/http"
	"strconv"
	"wskyygyso/casually/AppInit"
	"wskyygyso/casually/Model"
	"wskyygyso/casually/Validator"
)

type CommentTermsBannedService struct {
}

func (s CommentTermsBannedService) List(cxt *gin.Context) {
	list := Model.TtCommentTermsBannedList{}
	db := AppInit.GetDb().Table("tt_comment_banned_terms")
	//查询条件构建
	advertiserId := cxt.Query("advertiser_id")
	if advertiserId != "" {
		db = db.Where("advertiser_id=?", advertiserId)
	}
	term := cxt.Query("term")
	if term != "" {
		db = db.Where("term=?", term)
	}
	result := Model.NewResult()
	//分页
	var page Model.Page
	err := cxt.ShouldBindQuery(&page)
	if err != nil {
		result.Fail(err.Error())
		cxt.JSON(http.StatusOK, result)
		return
	}
	err = db.Count(&page.PageTotalCount).Error
	if err != nil {
		result.Fail(err.Error())
		cxt.JSON(http.StatusOK, result)
		return
	}

	page.PageTotal = page.PageTotalCount / page.PageSize
	if page.PageTotalCount%page.PageSize != 0 {
		page.PageTotal++
	}
	off := (page.Page - 1) * page.PageSize
	err = db.Offset(off).Limit(page.PageSize).Find(&list).Error
	if err != nil {
		result.Fail(err.Error())
		cxt.JSON(http.StatusOK, result)
		return
	}
	result.Data = gin.H{
		"list":     list,
		"pageInfo": page,
	}
	cxt.JSON(http.StatusOK, result)
}

func (s CommentTermsBannedService) Show(ctx *gin.Context) {
	model := Model.TtCommentTermsBanned{}
	db := AppInit.GetDb().Table("tt_comment_banned_terms")
	result := Model.NewResult()
	id := ctx.Param("id")
	checkId, err := strconv.Atoi(id)
	if err != nil {
		result.Fail(err.Error())
		ctx.JSON(http.StatusOK, result)
		return
	}
	var idValidator Validator.CommentTernsBannedShow
	idValidator.Id = checkId
	err = ctx.ShouldBind(&idValidator)
	if err != nil {
		result.Fail(err.Error())
		ctx.JSON(http.StatusOK, result)
		return
	}
	err = db.Where("id=?", idValidator.Id).First(&model).Error
	if err != nil {
		result.Fail(err.Error())
	}
	result.Data = model
	ctx.JSON(http.StatusOK, result)
}

func (s CommentTermsBannedService) Delete(ctx *gin.Context) {
	result := Model.NewResult()
	id := ctx.Param("id")
	checkId, err := strconv.Atoi(id)
	if err != nil {
		result.Fail(err.Error())
		ctx.JSON(http.StatusOK, result)
		return
	}
	var idValidator Validator.CommentTernsBannedShow
	idValidator.Id = checkId
	err = ctx.ShouldBind(&idValidator)
	if err != nil {
		result.Fail(err.Error())
		ctx.JSON(http.StatusOK, result)
		return
	}
	model := Model.TtCommentTermsBanned{}
	err = AppInit.GetDb().Table("tt_comment_banned_terms").Where("id=?", idValidator.Id).Delete(model).Error
	if err != nil {
		result.Fail(err.Error())
		ctx.JSON(http.StatusOK, result)
		return
	}
	result.Data = model
	ctx.JSON(http.StatusOK, result)
	return
}

func (s CommentTermsBannedService) Store(ctx *gin.Context) {
	result := Model.NewResult()
	var postData Validator.CommentTernsBannedStore
	err := ctx.ShouldBindJSON(&postData)
	if err != nil {
		result.Fail(err.Error())
		ctx.JSON(http.StatusOK, result)
		return
	}
	result.Data = postData
	ctx.JSON(http.StatusOK, result)
}
